﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using SqlCon;
namespace yfxt
{
    public partial class ms_cfcx : Form
    {




        int i;// 标志位


        public string s_temp;

        public string S_temp//页面传值参数
        {
            get { return s_temp; }

            set { this.s_temp = value; }
        }

        public ms_cfcx()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);


            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
            }
            catch { }


            SqlDataAdapter da = new SqlDataAdapter();
            if (i == 1)//i   为单选条件 用发票号码查询
            {

                if (tbn_fphm.Text.Trim() == "") { MessageBox.Show("请输入查询条件！"); return; }

                string s_001 = "select fphm from ms_cf01 where fphm='" + tbn_fphm.Text.ToString() + "'";
                using (SqlConnection c_001 = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
                {
                    if (c_001.State != ConnectionState.Open) { c_001.Open(); }

                    SqlCommand cmd_001 = new SqlCommand(s_001, c_001);

                    SqlDataReader sdr = cmd_001.ExecuteReader();

                    if (!sdr.HasRows)
                    {
                        MessageBox.Show("您所输入的发票号码不正确 ，请核实后再重新输入！");
                        tbn_fphm.Text = "";
                        return;
                    }

                    sdr.Close();
                }


                String SQLfphm = "select fphm,cfhm,brxm,fyrq," +
                                 "(case fybz when '0' then '未发药' when '1' then '已发药' end) as fybz,hjje,ygxm  " +
                                  "from ms_cf01,ms_cf02,gy_ygdm  where ms_cf01.cfsb=ms_cf02.cfsb and gy_ygdm.ygdm=ms_cf01.ysdm" +
                                  " and fphm=@fphm and fyrq between @fyrq1 and @fyrq2";


                SqlCommand cmd = new SqlCommand(SQLfphm, conn);

                cmd.Parameters.AddWithValue("@fphm", tbn_fphm.Text);
                cmd.Parameters.AddWithValue("@fyrq1", DateTime.Parse(dateTimePicker1.Text));
                cmd.Parameters.AddWithValue("@fyrq2", DateTime.Parse(dateTimePicker2.Text));

                da.SelectCommand = cmd;
            }
            else if (i == 2)//用病人姓名查询
            {
                if (tbn_brxm.Text.Trim() == "") { MessageBox.Show("请输入查询条件！"); return; }
                string s_001="select brxm from ms_cf01 where brxm='"+tbn_brxm.Text.ToString()+"'";
                using(SqlConnection c_001=new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
                {
                    if (c_001.State != ConnectionState.Open) { c_001.Open(); }

                    SqlCommand cmd_001 = new SqlCommand(s_001,c_001);

                    SqlDataReader sdr = cmd_001.ExecuteReader();

                    if(!sdr.HasRows)
                    {
                        MessageBox.Show("您所输入的病人姓名不正确 ，请核实后再重新输入！");
                        tbn_brxm.Text = "";
                        return;
                    }

                    sdr.Close();
                }




                String SQLfphm = "select fphm,cfhm,brxm,fyrq," +
                                 "(case fybz when '0' then '未发药' when '1' then '已发药' end) as fybz,hjje,ygxm  " +
                                  "from ms_cf01,ms_cf02,gy_ygdm  where ms_cf01.cfsb=ms_cf02.cfsb and gy_ygdm.ygdm=ms_cf01.ysdm" +
                                  " and brxm=@brxm and fyrq between @fyrq1 and @fyrq2";


                SqlCommand cmd = new SqlCommand(SQLfphm, conn);

                cmd.Parameters.AddWithValue("@brxm", tbn_brxm.Text);
                cmd.Parameters.AddWithValue("@fyrq1", DateTime.Parse(dateTimePicker1.Text));
                cmd.Parameters.AddWithValue("@fyrq2", DateTime.Parse(dateTimePicker2.Text));

                da.SelectCommand = cmd;
            }

            else
            { //用处方号码查询

                if (tbn_cfhm.Text.Trim() == "") { MessageBox.Show("请输入查询条件！"); return; }


                string s_001 = "select cfhm from ms_cf01 where cfhm='" + tbn_cfhm.Text.ToString() + "'";
                using (SqlConnection c_001 = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
                {
                    if (c_001.State != ConnectionState.Open) { c_001.Open(); }

                    SqlCommand cmd_001 = new SqlCommand(s_001, c_001);

                    SqlDataReader sdr = cmd_001.ExecuteReader();

                    if (!sdr.HasRows)
                    {
                        MessageBox.Show("您所输入的处方号码不正确 ，请核实后再重新输入！");
                        tbn_fphm.Text = "";
                        return;
                    }

                    sdr.Close();
                }




                String SQLfphm = "select fphm,cfhm,brxm,fyrq," +
                                 "(case fybz when '0' then '未发药' when '1' then '已发药' end) as fybz,hjje,ygxm  " +
                                  "from ms_cf01,ms_cf02,gy_ygdm  where ms_cf01.cfsb=ms_cf02.cfsb and gy_ygdm.ygdm=ms_cf01.ysdm" +
                                  " and cfhm=@cfhm and fyrq between @fyrq1 and @fyrq2";

                SqlCommand cmd = new SqlCommand(SQLfphm, conn);

                cmd.Parameters.AddWithValue("@cfhm", tbn_cfhm.Text);
                cmd.Parameters.AddWithValue("@fyrq1", DateTime.Parse(dateTimePicker1.Text));
                cmd.Parameters.AddWithValue("@fyrq2", DateTime.Parse(dateTimePicker2.Text));

                da.SelectCommand = cmd;
            }

            DataTable dt = new DataTable();


            da.Fill(dt);

            dataGridView1.DataSource = dt;


            dataGridView1.Columns[0].HeaderCell.Value = "发票号码";
            dataGridView1.Columns[1].HeaderCell.Value = "处方号码";
            dataGridView1.Columns[2].HeaderCell.Value = "病人姓名";
            dataGridView1.Columns[3].HeaderCell.Value = "发药日期";
            dataGridView1.Columns[4].HeaderCell.Value = "是否发药";
            dataGridView1.Columns[5].HeaderCell.Value = "合计金额";
            dataGridView1.Columns[6].HeaderCell.Value = "医生姓名";


            label8.Text = (dataGridView1.Rows.Count - 1).ToString();//获取处方的张数


            tbn_fyck.Text = "窗口一";
            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            tbn_brxm.Text = "";
            tbn_cfhm.Text = "";
            tbn_fphm.Text = "";
            tbn_fyck.Text = "";
            dateTimePicker1.Text = "";
            dateTimePicker2.Text = "";


            DataTable dt = (DataTable)dataGridView1.DataSource;
            dt.Rows.Clear();
            dataGridView1.DataSource = dt;

            label8.Text = "0";





        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0) { return; }
            s_temp = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();//取得选中的处方号码然后传值给子窗口

            ms_cfcx_mx cfcx_mx = new ms_cfcx_mx();

            cfcx_mx.Owner = this;

            cfcx_mx.ShowDialog();
        }
        private void button5_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tbn_brxm_TextChanged(object sender, EventArgs e)
        {
            tbn_cfhm.Text = "";
            tbn_fphm.Text = "";

            i = 2;
        }

        private void tbn_fphm_TextChanged(object sender, EventArgs e)
        {
            tbn_brxm.Text = "";
            tbn_cfhm.Text = "";
            i = 1;
        }

        private void tbn_cfhm_TextChanged(object sender, EventArgs e)
        {
            tbn_brxm.Text = "";
            tbn_fphm.Text = "";

            i = 3;
        }

        private void ms_cfcx_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“dataSet1.V_ms_cf01_cf02_ygdm”中。您可以根据需要移动或移除它。
            //this.v_ms_cf01_cf02_ygdmTableAdapter.Fill(this.dataSet1.V_ms_cf01_cf02_ygdm);
            tbn_fyck.Text = "窗口一";
            tbn_fyck.Items.Add("窗口一");
            tbn_fyck.Items.Add("窗口二");
            tbn_fyck.Items.Add("窗口三");
        }

        private void ms_cfcx_Load_1(object sender, EventArgs e)
        {
            tbn_fyck.Text = "窗口一";
            tbn_fyck.Items.Add("窗口一");
            tbn_fyck.Items.Add("窗口二");
            tbn_fyck.Items.Add("窗口三");

            this.timer1.Interval = 100;
            this.timer1.Enabled = true;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(dataGridView1.Rows.Count==0)
            {
            foreach (DataGridViewRow drw in dataGridView1.Rows)//显示每一行数
            {
                for (int i0 = 0; i0 < dataGridView1.Rows.Count; i0++)
                    dataGridView1.Rows[i0].HeaderCell.Value = Convert.ToString(i0 + 1);
            }
            }
        }
    }
}
